package com.aek.aekdroid;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;

public class MatchUpdaterService extends IntentService {
	// public static final String MATCH_UPDATE = "com.aek.aekdroid.NEW_UPDATES";
	public static final String TAG = "MatchUpaterService";
	public static final String NEW_UPDATES_COUNT = "NEW_UPDATES_COUNT";
	public static final String MANUAL = "MANUAL";
	public static final String RECEIVE_NOTIFICATIONS = "com.aek.aekdroid.RECEIVE_NOTIFICATIONS";
	private NotificationManager notificationManager;
	private Notification notification;

	public MatchUpdaterService() {
		super(TAG);
		MyLog.d(TAG, "Created MatchUpdaterService");
	}

	@Override
	protected void onHandleIntent(Intent inIntent) {
		Intent intent = new Intent(ScheduleActivity.NEW_UPDATES);
		boolean manual = inIntent.getBooleanExtra(MANUAL, false);
		intent.putExtra(MANUAL, manual);
		this.notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		this.notification = new Notification(R.drawable.ic_notify_match, "", 0);
		AekDroidApplication aek = (AekDroidApplication) getApplication();
		MyLog.d(TAG, "Getting WebAekDroid obj");
		if (aek.getWebAekDroid().isConnected) {
			int updates = aek.fetchUpdates();
			MyLog.d(TAG, updates + " updates");
			intent.putExtra(NEW_UPDATES_COUNT, updates);
			sendBroadcast(intent, RECEIVE_NOTIFICATIONS);
			if (updates > 0) {
				sendMatchNotification(updates, aek);
			}
			if (aek.isCheckUpdate() && !AekDroidApplication.newVersion.equals("")) {
				Intent checkUpdate = new Intent(this, UpdateChecker.class);
				startService(checkUpdate);
			}
		} else {
			intent.putExtra(NEW_UPDATES_COUNT, -1);
			sendBroadcast(intent, RECEIVE_NOTIFICATIONS);
			MyLog.e(TAG, "No connection to the internet");

		}
		MyLog.d(TAG, "onHandlingIntent");
	}

	private void sendMatchNotification(int updates, AekDroidApplication aek) {
		MyLog.d(TAG, "Sending notification for " + updates + " updates");
		Intent intent = new Intent(this, ScheduleActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, -1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		this.notification.when = System.currentTimeMillis();
		this.notification.flags = Notification.FLAG_AUTO_CANCEL;
		this.notification.number = updates;
		if (aek.isVibrate()) {
			// this.notification.vibrate = new long[] { 500, 500, 500, 500, 200,
			// 800, 200, 1600 };
			this.notification.defaults |= Notification.DEFAULT_VIBRATE;
			this.notification.defaults |= Notification.DEFAULT_LIGHTS;
			this.notification.defaults |= Notification.DEFAULT_SOUND;
		}
		CharSequence notificationTitle = this.getText(R.string.msgNotificationTitle);
		CharSequence notificationSummary = this.getString(R.string.msgNotificationMessage, updates);
		this.notification.setLatestEventInfo(this, notificationTitle, notificationSummary, pendingIntent);
		this.notificationManager.notify(0, this.notification);
		MyLog.d(TAG, "Send Notification");

	}
}
